<?php 
class Zen_Behavior_Aliasfull extends Zen_Behavior_Model {
	
	public function afterChange() {
		$this->_updatePath ();
	}
	
	private function _updatePath() {
		$model = $this->getModel();
		$data = $model->select ()->setColumns ( array ('tree' => array ('id', 'parent_id', 'left', 'right','level' ), 'page' => array ('alias', 'label' ) ) )->setOrder('tree.left')->setDepth ( 0 )->getData ();
		$alias = array();
		$types=$model->getTypes();
		$ItemModel = $types['page'] ['model'];
		foreach ( $data as $node ) {
			ksort( $alias, SORT_NUMERIC );
			$level = $node['level'];
			if ( isset($alias[$level]) ) {
				$count = count($alias);
				for ($i = $level; $i<=$count; $i++ ) {
					unset ($alias[$i]);
				}
			}
			$alias[$level]=$node['alias'];
			$aliasfull="/".implode('/', $alias).'/';	
			$aliasfull=preg_replace('/\\/\\//', '/', $aliasfull);
			$res = $ItemModel->update($node['id'],array('aliasfull'=>$aliasfull));	
		}
		
	}
		
}